package com.libraries.helper;

import com.example.libraries.R;

import android.content.Context;
import android.os.Handler;
import android.widget.Button;

public class VerifyCodeHelper {
	private static final String TAG="VerifyCodeHelper";
	private static Handler verifyHandler;
	private static Button btn_verifyCode;
	
	public static boolean isLooping;
	private static boolean stopLoop;
	private static int timerCount=60;
	private static String resendVerifyCode;
	
	public static void startWaitting(Context context,Button btn_verifyCode){
		Logger.log(TAG, "startWaitting", context,btn_verifyCode);
		if (isLooping) {
			return;
		}
		resetVarible(false);
		resendVerifyCode=context.getResources().getString(R.string.resend_msg);
		VerifyCodeHelper.btn_verifyCode=btn_verifyCode;
		loopWaitting();
	}
	
	public static void loopWaitting(){
//		Logger.log(TAG, "loopWaitting", timerCount);
		if (verifyHandler==null) {
			verifyHandler=new Handler();
		}
		verifyHandler.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				Logger.log("loopWaitting", "run", timerCount,stopLoop);
				if (stopLoop) {
					return;
				}
				timerCount--;
				if (timerCount!=0) {
					loopWaitting();
					btn_verifyCode.setText("	"+timerCount+"	");
				}
				else{
					resetVarible(true);
				}
			}
		}, 1000);
	}
	
	private static void resetVarible(boolean resend){
		timerCount=60;
		stopLoop=false;
		isLooping=resend?false:true;
		if (resend) {
			btn_verifyCode.setText(resendVerifyCode);
		}
	}
	
	public static void stopWaitting(){
		timerCount=60;
		isLooping=false;
		stopLoop=true;
	}
	
	
}
